package com.hongsheng.mapper;

/*
@Author：郭振文
@Package：com.hongsheng.mapper
@File：RoleMapper
@Date：2023/8/10 23:47
*/

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hongsheng.pojo.RA;
import com.hongsheng.pojo.Role;
import com.hongsheng.util.RoleSqlProvider;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface RoleMapper extends BaseMapper<Role> {

    //根据userId查询角色信息
    @Select("select * from tb_user_role ur,tb_role r where ur.r_id=r.id and ur.u_id=#{id}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "role_name",property = "roleName"),
            @Result(column = "department",property = "department"),
            @Result(column = "creat_time",property = "createTime"),
            @Result(column = "last_time",property = "lastTime"),
            //配置authority
            @Result(
                    javaType = List.class,
                    property = "authorities",
                    column = "id",
                    many = @Many(select = "com.hongsheng.mapper.AuthorityMapper.selectAuthorityByRoleId")
            )
    })
    List<Role> selectRoleByUserId(Integer id);

    //查询所有角色信息
    @SelectProvider(type = RoleSqlProvider.class,method = "getRoleByCondition")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "role_name",property = "roleName"),
            @Result(column = "department",property = "department"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "last_time",property = "lastTime"),
            //配置authority
            @Result(
                    javaType = List.class,
                    property = "authorities",
                    column = "id",
                    many = @Many(select = "com.hongsheng.mapper.AuthorityMapper.selectAuthorityByRoleId")
            )
    })
    List<Role> selectRole(@Param("roleName") String roleName,
                          @Param("department") String department);

    //根据id查询角色信息
    @Select("select * from tb_role where id=#{id}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "role_name",property = "roleName"),
            @Result(column = "department",property = "department"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "last_time",property = "lastTime"),
            //配置authority
            @Result(
                    javaType = List.class,
                    property = "authorities",
                    column = "id",
                    many = @Many(select = "com.hongsheng.mapper.AuthorityMapper.selectAuthorityByRoleId")
            )
    })
    Role selectRoleById(Integer id);

    @Update("update tb_role set role_name=#{roleName}, department=#{department} WHERE id=#{id}")
    Boolean updateRole(Role role);

    @Insert("insert into tb_authority_role (r_id,a_id) values (#{rid},#{aid})")
    Boolean insertRoleAuthority(RA ra);

    @Select("select * from tb_role")
    List<Role> selectRole1();

    @Delete("delete from tb_authority_role where r_id=#{rid} and a_id=#{aid}")
    Boolean deleteRoleAuthority(RA ra);

    @Select("select * from tb_authority_role where r_id=#{rid} and a_id=#{aid}")
    List<RA> selectRA(RA ra);




}
